package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.Userpage;
import com.example.pinkcat.activity.VideoActivity;
import com.example.pinkcat.activity.VideoPinkcat;
import com.example.pinkcat.activity.api.entity.ResponsEntity_video;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.api.entity.MyFanEntity;
import com.example.pinkcat.api.entity.ResponsEntity_Myfanentity;
import com.google.gson.Gson;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SP_recyleview_adapter extends RecyclerView.Adapter<SP_recyleview_adapter.ViewHolder>{
    private final Context context;
    int pl_numbers;
    int type=0;//0-----pinCatVideo; 1--2--3--4-影视； 5-----用户
    List<VideoEntity> videoEntities0=new ArrayList<>();
    List<VideoEntity> videoEntities1=new ArrayList<>();
    List<VideoEntity> videoEntities2=new ArrayList<>();
    List<VideoEntity> videoEntities3=new ArrayList<>();
    List<VideoEntity> videoEntities4=new ArrayList<>();
    List<MyFanEntity> userlist_5=new ArrayList<>();


    //构造方法
    public SP_recyleview_adapter(Context context, int pl_numbers, int type){
        this.context=context;
        this.pl_numbers=pl_numbers;
        this.type=type;
    }
    //0-----pinCatVideo; 1--2--3--4-影视； 5-----用户
    public void setData(String jsonStr,int type){
        Gson gson= new Gson();
        ResponsEntity_video ra0=gson.fromJson(jsonStr, ResponsEntity_video.class);
        switch (type){
            case 0:this.videoEntities0=ra0.getData();break;
            case 1:this.videoEntities1=ra0.getData();break;
            case 2:this.videoEntities2=ra0.getData();break;
            case 3:this.videoEntities3=ra0.getData();break;
            case 4:this.videoEntities4=ra0.getData();break;
            case 5:ResponsEntity_Myfanentity ra5=gson.fromJson(jsonStr, ResponsEntity_Myfanentity.class);this.userlist_5=ra5.getData();break;
            default:break;
        }
        notifyDataSetChanged();
    }

    public List<VideoEntity> getVideosData(int position){
        switch (position){
            case 0:return videoEntities0;
            case 1:return videoEntities1;
            case 2:return videoEntities2;
            case 3:return videoEntities3;
            case 4:return videoEntities4;
            default:return null;
        }
    }

    public void setVideosData(List<VideoEntity> videoEntities,int position){
        switch (position){
            case 0:this. videoEntities0=videoEntities;break;
            case 1:this. videoEntities1=videoEntities;break;
            case 2:this. videoEntities2=videoEntities;break;
            case 3:this. videoEntities3=videoEntities;break;
            case 4:this. videoEntities4=videoEntities;break;
            default:break;
        }
        notifyDataSetChanged();
    }
    public  List<MyFanEntity> getUserData(){
        return userlist_5;
    }
    public  void setUserData(List<MyFanEntity> myFanEntities){
        this.userlist_5=myFanEntities;
        notifyDataSetChanged();
    }

    //内部内
    public static class ViewHolder extends RecyclerView.ViewHolder{
        //userview
        RoundedImageView user_head;
        TextView user_name;
        TextView howMuchFans;
        TextView howMuchVideo;
        TextView followbtn;

        //tv
        RoundedImageView face_animation;
        TextView title_animation;
        TextView time_animation;
        TextView type_animation;
        TextView area_animation;
        TextView videoType_animation;
        TextView play_num;
        TextView btn_viewNow;
        TextView followbtn_animation;

        //video
        RoundedImageView video_face;
        TextView about_v_title;
        TextView about_username;
        TextView about_bfsl;
        TextView about_plsl;
        View list_main_body;


        //寻找声明布局控件
        public ViewHolder(View view) {
            super(view);
            video_face=view.findViewById(R.id.video_face);
            about_v_title=view.findViewById(R.id.about_v_title);
            about_username=view.findViewById(R.id.about_username);
            about_bfsl=view.findViewById(R.id.about_bfsl);
            about_plsl=view.findViewById(R.id.about_plsl);
            list_main_body=view.findViewById(R.id.list_main_body);

            user_head=view.findViewById(R.id.user_head);
            user_name=view.findViewById(R.id.user_name);
            howMuchFans=view.findViewById(R.id.howMuchFans);
            howMuchVideo=view.findViewById(R.id.howMuchVideo);
            followbtn=view.findViewById(R.id.followbtn);

            face_animation=view.findViewById(R.id.face_animation);
            title_animation=view.findViewById(R.id.title_animation);
            time_animation=view.findViewById(R.id.time_animation);
            type_animation=view.findViewById(R.id.type_animation);
            area_animation=view.findViewById(R.id.area_animation);
            videoType_animation=view.findViewById(R.id.videoType_animation);
            play_num=view.findViewById(R.id.play_num);
            btn_viewNow=view.findViewById(R.id.btn_viewNow);
            followbtn_animation=view.findViewById(R.id.followbtn_animation);
        }
    }

    //创建获取 view  布局
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取item内容
        View view;
        if (type==0)
            view= LayoutInflater.from(context).inflate(R.layout.list_aboutvideo,parent,false);
        else if (type==1||type==2||type==3||type==4)
            view= LayoutInflater.from(context).inflate(R.layout.list_animation,parent,false);
        else
            view= LayoutInflater.from(context).inflate(R.layout.list_userlist_for_search,parent,false);
        return new ViewHolder(view);
    }

    //绑定数据
    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (type==0){
            setVideo(holder,position);
        }else if (type==1){
            setZy(holder,position);
        }else if (type==2){
            setDm(holder,position);
        }else if (type==3){
            setTv(holder,position);
        }else if (type==4){
            setFilm(holder,position);
        }else if (type==5){
            setUser(holder,position);
        }
    }

    @Override
    public int getItemCount() {
        switch (type){
            case 0:return videoEntities0.size();
            case 1:return videoEntities1.size();
            case 2:return videoEntities2.size();
            case 3:return videoEntities3.size();
            case 4:return videoEntities4.size();
            case 5:return userlist_5.size();
            default:return 0;
        }
    }


    @SuppressLint("SetTextI18n")
    void setUser(ViewHolder holder, int position){
        Glide.with(context).load(Uri.parse(userlist_5.get(position).getHead_photo())).into(holder.user_head);
        holder.user_name.setText(userlist_5.get(position).getUser_name());
        if (userlist_5.get(position).isIsvip())
            holder.user_name.setTextColor(context.getResources().getColor(R.color.pink));
        holder.howMuchFans.setText(userlist_5.get(position).getFan_count()+"粉丝");
        holder.howMuchVideo.setText(userlist_5.get(position).getVideo_count()+"视频");
        if (userlist_5.get(position).isIskeep()){
            holder.followbtn.setText(R.string.unfollow);
            holder.followbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
        }else {
            holder.followbtn.setText(R.string.follow);
            holder.followbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
        }
        holder.user_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Userpage.class);
                intent.putExtra("up_id", userlist_5.get(position).getUser_id());
                context.startActivity(intent);
            }
        });
        //关注与取关
        holder.followbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.followbtn.getText().toString().equals("+ 关 注")){
                    if (GetData.followOrUnfollow(context,userlist_5.get(position).getUser_id(),true)){
                        holder.followbtn.setText(R.string.unfollow);
                        holder.followbtn.setTextColor(context.getResources().getColor(R.color.black_overlay));
                        holder.followbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
                    }
                }
                else {
                    if (GetData.followOrUnfollow(context,userlist_5.get(position).getUser_id(),false)){
                        holder.followbtn.setText(R.string.follow);
                        holder.followbtn.setTextColor(context.getResources().getColor(R.color.white));
                        holder.followbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
                    }
                }
            }
        });
    }

    void setVideo(ViewHolder holder, int position){
        Glide.with(context).load(Uri.parse(videoEntities0.get(position).getVideo_cover())).into(holder.video_face);
        holder.about_v_title.setText(videoEntities0.get(position).getVideo_name());
        holder.about_username.setText(videoEntities0.get(position).getUser_name());
        holder.about_bfsl.setText(String.valueOf(videoEntities0.get(position).getTotalplay()));
        holder.about_plsl.setText(String.valueOf(videoEntities0.get(position).getComment_count()));
        holder.list_main_body.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent;
                if (videoEntities0.get(position).getVideo_type().equals("综艺")||
                        videoEntities0.get(position).getVideo_type().equals("电视剧")||
                        videoEntities0.get(position).getVideo_type().equals("电影")||
                        videoEntities0.get(position).getVideo_type().equals("动漫")){
                    intent=new Intent(context, VideoPinkcat.class);
                }else {
                    intent=new Intent(context, VideoActivity.class);
                }
                intent.putExtra("v_id",videoEntities0.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities0.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities0.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities0.get(position).getVideo_lab());
                context.startActivity(intent);

            }
        });
    }

    void setZy(ViewHolder holder, int position){
        Glide.with(context).load(Uri.parse(videoEntities1.get(position).getVideo_cover())).into(holder.face_animation);
        holder.title_animation.setText(videoEntities1.get(position).getVideo_name());
        SimpleDateFormat format =  new SimpleDateFormat("yyyy");
        holder.time_animation.setText(format.format(videoEntities1.get(position).getVideo_pub_time()));
        holder.type_animation.setText(videoEntities1.get(position).getVideo_type());
        holder.area_animation.setText("未知地区");
        holder.videoType_animation.setText(videoEntities1.get(position).getVideo_lab());
        holder.play_num.setText(String.valueOf(videoEntities1.get(position).getTotalplay()));
        holder.followbtn_animation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, VideoPinkcat.class);
                intent.putExtra("v_id",videoEntities1.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities1.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities1.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities1.get(position).getVideo_lab());
                context.startActivity(intent);
            }
        });
        holder.btn_viewNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, VideoPinkcat.class);
                intent.putExtra("v_id",videoEntities1.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities1.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities1.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities1.get(position).getVideo_lab());
                context.startActivity(intent);
            }
        });
    }
    void setDm(ViewHolder holder, int position){
        Glide.with(context).load(Uri.parse(videoEntities2.get(position).getVideo_cover())).into(holder.face_animation);
        holder.title_animation.setText(videoEntities2.get(position).getVideo_name());
        SimpleDateFormat format =  new SimpleDateFormat("yyyy");
        holder.time_animation.setText(format.format(videoEntities2.get(position).getVideo_pub_time()));
        holder.type_animation.setText(videoEntities2.get(position).getVideo_type());
        holder.area_animation.setText("未知地区");
        holder.videoType_animation.setText(videoEntities2.get(position).getVideo_lab());
        holder.play_num.setText(String.valueOf(videoEntities2.get(position).getTotalplay()));
        holder.followbtn_animation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, VideoPinkcat.class);
                intent.putExtra("v_id",videoEntities2.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities2.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities2.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities2.get(position).getVideo_lab());
                context.startActivity(intent);
            }
        });
        holder.btn_viewNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, VideoPinkcat.class);
                intent.putExtra("v_id",videoEntities2.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities2.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities2.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities2.get(position).getVideo_lab());
                context.startActivity(intent);
            }
        });
    }
    void setTv(ViewHolder holder, int position){
        Glide.with(context).load(Uri.parse(videoEntities3.get(position).getVideo_cover())).into(holder.face_animation);
        holder.title_animation.setText(videoEntities3.get(position).getVideo_name());
        SimpleDateFormat format =  new SimpleDateFormat("yyyy");
        holder.time_animation.setText(format.format(videoEntities3.get(position).getVideo_pub_time()));
        holder.type_animation.setText(videoEntities3.get(position).getVideo_type());
        holder.area_animation.setText("未知地区");
        holder.videoType_animation.setText(videoEntities3.get(position).getVideo_lab());
        holder.play_num.setText(String.valueOf(videoEntities3.get(position).getTotalplay()));
        holder.followbtn_animation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Intent intent=new Intent(context, VideoPinkcat.class);
                    intent.putExtra("v_id",videoEntities3.get(position).getVideo_id());
                    intent.putExtra("up_id",videoEntities3.get(position).getUser_id());
                    intent.putExtra("v_link",videoEntities3.get(position).getVideo_link());
                    intent.putExtra("v_labs",videoEntities3.get(position).getVideo_lab());
                    context.startActivity(intent);
            }
        });
        holder.btn_viewNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, VideoPinkcat.class);
                intent.putExtra("v_id",videoEntities3.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities3.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities3.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities3.get(position).getVideo_lab());
                context.startActivity(intent);
            }
        });
    }

    void setFilm(ViewHolder holder, int position){
        Glide.with(context).load(Uri.parse(videoEntities4.get(position).getVideo_cover())).into(holder.face_animation);
        holder.title_animation.setText(videoEntities4.get(position).getVideo_name());
        SimpleDateFormat format =  new SimpleDateFormat("yyyy");
        holder.time_animation.setText(format.format(videoEntities4.get(position).getVideo_pub_time()));
        holder.type_animation.setText(videoEntities4.get(position).getVideo_type());
        holder.area_animation.setText("未知地区");
        holder.videoType_animation.setText(videoEntities4.get(position).getVideo_lab());
        holder.play_num.setText(String.valueOf(videoEntities4.get(position).getTotalplay()));
        holder.followbtn_animation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, VideoPinkcat.class);
                intent.putExtra("v_id",videoEntities4.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities4.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities4.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities4.get(position).getVideo_lab());
                context.startActivity(intent);
            }
        });
        holder.btn_viewNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, VideoPinkcat.class);
                intent.putExtra("v_id",videoEntities4.get(position).getVideo_id());
                intent.putExtra("up_id",videoEntities4.get(position).getUser_id());
                intent.putExtra("v_link",videoEntities4.get(position).getVideo_link());
                intent.putExtra("v_labs",videoEntities4.get(position).getVideo_lab());
                context.startActivity(intent);
            }
        });
    }
}
